<template>
	<view class="container">
		<lk-topbar :title="'运动症状监测'" :backcolor="'#FFFFFF'" @back="back"></lk-topbar>
		<lk-recording v-if="type == 'recording'" ref='recording'  :sindex="sindex" :last="isLast"  @changeindex="change" ></lk-recording>
		<lk-action v-if="type == 'action'"  ref='action'  :isfinish="isFinish"  :type="type" pageType="mntr" :last="isLast" :sindex="sindex" :className='className'
			:mirrorImage="mirrorImage" :introduction="introduction"  @changeindex="change"></lk-action>
		<lk-game v-if="type == 'game'"  ref='game' :sindex="sindex"  :mirrorImage="mirrorImage"  :issingle="isSingle"  :last="isLast"  @changeindex="change" :introduction="introduction" ></lk-game>
	</view>
</template>

<script>
	import lkRecording from "../../components/lk-recording/lk-recording.vue"
	import lkAction from "../../components/lk-action/index.vue"
	import lkGame from "../../components/lk-game/index.vue"
	import Audio from '../../core/audio.js'
	import equipment from '../../core/equipment.js'
	export default {
		name: "",
		data() {
			return {
				isFinish:false,
				para:[{
					name:"1",
					title:'',
					type:'recording', //type类型
					variable:'voicePath', //字段
					audio:'', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"2-1", 
					title:'睁眼站立',
					type:'action', //type类型
					variable:'balance_open_eye', //字段
					audio:'monitorBalanceOpenEye', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'C-06', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"2-2", 
					title:'闭眼站立',
					type:'action', //type类型
					variable:'balance_close_eye', //字段
					audio:'monitorBalanceCloseEye', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'C-07', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"3-1-L",
					title:'左手-震颤',
					type:'action', //type类型
					variable:'left_upper_limb', //字段
					audio:'monitorActionPromptTLeft', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'L-01', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"3-1-R",
					title:'右手-震颤',
					type:'action', //type类型
					variable:'right_upper_limb', //字段
					audio:'monitorActionPromptTRight', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'R-01', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"3-2-L",
					title:'左侧肢体-震颤',
					type:'action', //type类型
					variable:'left_tremor', //字段
					audio:'screeningActionPromptTLeft', //音频
					mirror:true, //是否镜像
					condition:false, //是否动态
					className:'R-02-05', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"3-2-R",
					title:'右侧肢体-震颤',
					type:'action', //type类型
					variable:'right_tremor', //字段
					audio:'screeningActionPromptTRight', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'R-02-05', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"4-1-L",  
					title:'左手点击-迟缓',
					type:'game', //type类型
					variable:'game_performance_single_left', //字段
					audio:'monitorGameLeft', //音频
					mirror:true, //是否镜像
					condition:false, //是否动态   电气泡
					className:'', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"4-1-R", 
					title:'右手点击-迟缓',
					type:'game', //type类型
					variable:'game_performance_single_right', //字段
					audio:'monitorGameRight', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'', // 类名
					ortherClassName:'', //另一个类名
				},{
					name:"4-2-L",
					title:'左手点击-迟缓',
					type:'game', //type类型
					variable:'game_performance_dual_left', //字段
					audio:'screeningGameLeft', //音频
					mirror:true, //是否镜像
					condition:true, //是否动态   电气泡是否单个
					className:'', // 类名
					ortherClassName:'', //另一个类名
				},
				{
					name:"4-2-R",
					title:'右手点击-迟缓',
					type:'game', //type类型
					variable:'game_performance_dual_right', //字段
					audio:'screeningGameRight', //音频
					mirror:false, //是否镜像
					condition:true, //是否动态
					className:'', // 类名
					ortherClassName:'', //另一个类名
				},{
					name:"4-3-L",
					title:'跺左脚-迟缓',
					type:'action', //type类型
					variable:'left_brady', //字段
					audio:'screeningActionPromptSLeft', //音频
					mirror:true, //是否镜像
					condition:false, //是否动态
					className:'R-02-05', // 类名
					ortherClassName:'R-05-2', //另一个类名
				},
				{
					name:"4-3-R",
					title:'跺右脚-迟缓',
					type:'action', //type类型
					variable:'right_brady', //字段
					audio:'screeningActionPromptSRight', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'R-02-05', // 类名
					ortherClassName:'R-05-2', //另一个类名
				},{
					name:"5", 
					title:'五米往返行走-步态',
					type:'action', //type类型
					variable:'walk', //字段
					audio:'gait', //音频
					mirror:false, //是否镜像
					condition:false, //是否动态
					className:'C-12', // 类名
					ortherClassName:'', //另一个类名
				}],
				allType:[],
				// type: "",
				className: '',
				mirrorImage: false,
				introduction: "",
				sindex: 0,
				sample:{},
				variable:'',
				isSingle:false,
				isLast:false,
				isClick:true,
			}
		},
		components: {
			lkRecording,
			lkAction,
			lkGame
		},
		computed:{
			type(){
				let type = '';
				this.para.forEach(item=>{
					if(item.name === this.allType[this.sindex]){
						
						if(item.name === '5'){
							this.isFinish = true;
						}else{
							this.isFinish = false;
						}
						
						type = item.type;
						this.variable = item.variable;
						
						this.introduction = item.title;
						if(item.type == 'action'){
							Audio.audio(item.audio,()=>{});
							this.mirrorImage = item.mirror;
							this.className = item.className;
							if(item.condition){
								this.timer = setInterval(()=>{
									if(this.className == item.className){
										this.className = item.ortherClassName
									}else{
										this.className = item.className;
									}
								},500)
							}
						}
						if(item.type == 'game'){
							Audio.audio(item.audio,()=>{});
							this.mirrorImage = item.mirror;
							this.isSingle = item.condition;
							this.className = item.className;
						}
					}
				})
				this.isLast = this.sindex == this.allType.length-1 ? true : false;
				return type
			}
		},
		onLoad(o) {
			this.allType = o.typeArr.split(',');
		},
		methods: {
			// 返回
			back(){
				let _this = this, itemList = [];
				if(_this.type == 'recording' || this.isLast){
					itemList = ['退出测试','重新测试']
				}else{
					itemList = ['退出测试','重新测试','下一项']
				}
				uni.showActionSheet({
					itemList:itemList,
					success(res) {
						Audio.audioCtx.stop();
						if(res.tapIndex == 0){
							uni.showModal({
								title:'提示',
								content:"是否确定退出",
								success(res){
									if(res.confirm){
										_this.$routeJump.back()
									}
								}
							})
						}else if(res.tapIndex == 1){
							uni.showModal({
								title:'提示',
								content:"是否确定重头测试",
								success(res){
									if(res.confirm){
										
										uni.showLoading({
											title:"加载中..."
										})
										_this.sindex = -1;
										_this.className= '';
										_this.mirrorImage= false;
										_this.introduction= "";
										_this.variable='';
										_this.isSingle=false;
										_this.isLast=false;
										setTimeout(res=>{
											uni.hideLoading();
											_this.sindex = 0;
											_this.sample = {};
										},700)
									}
								}
							})
							
						}else{
							_this.$refs[_this.type].next()
						}
					}
				})
				
			},
			/** 下一项 或 完成测试
			 * @param {Object} o
			 */
			async change(o){
				let _this = this;
				_this.sample[_this.variable] = o.data;
				_this.sindex = -1;
				uni.showLoading({
					title:"加载中..."
				})
				if(_this.isLast){
				
				let res = await _this.$utils.request({
						url:'/patient/symptom_monitor/',
						method:"POST",
						data:{
							test_data:{
								left_upper_limb:_this.sample.left_upper_limb,
								right_upper_limb:_this.sample.right_upper_limb,
								left_tremor:_this.sample.left_tremor,
								right_tremor:_this.sample.right_tremor,
								left_brady:_this.sample.left_brady,
								right_brady:_this.sample.right_brady,
								balance_open_eye:_this.sample.balance_open_eye,
								balance_close_eye:_this.sample.balance_close_eye,
								walk:_this.sample.walk,
								game_performance_single_left:_this.sample.game_performance_single_left,
								game_performance_single_right:_this.sample.game_performance_single_right,
								game_performance_dual_left:_this.sample.game_performance_dual_left,
								game_performance_dual_right:_this.sample.game_performance_dual_right,
							}
						}
					})
					
						if(_this.allType.indexOf('1') != -1){
							
							for(let i in _this.sample.voicePath){
								let url = await _this.upfile(_this.sample.voicePath[i],res.data.id,i);
								if(url.statusCode == 422){
									uni.showToast({
										title:"上传失败",
										icon:"error"
									})
									break;
								}
							}
								uni.hideLoading();
								uni.showToast({
									title:"所有测试完毕",
									success(res){
									
									}
								})
								setTimeout(()=>{
										_this.$routeJump.reLaunch('/pages/index/index');
								},3000)
						}else{
							uni.hideLoading();
							uni.showToast({
								title:"所有测试完毕",
								success(res){
								
								}
							})
							setTimeout(()=>{
								_this.$routeJump.reLaunch('/pages/index/index');
							},3000)
						}
				
				}else{
					setTimeout(()=>{
						uni.hideLoading();
						this.sindex = o.sindex;
					},700)
				}
			},
			upfile(url,id,name) {
				let _this = this;
				let token = uni.getStorageSync('token'),
				header = {
					'content-type':'audio/mp3',
					'cookie': uni.getStorageSync('cookie'),
					'X-CSRFToken': token
				}
				
			  return new Promise((resolve, reject) => {
			    wx.uploadFile({
			      filePath: url,
			      name: id+name,
			      url: _this.$utils.api+'/patient/symptom_voice/'+id,
						header,
			      success(res) {
							console.log(res);
			        resolve(res)
			      },
			      fail(err) {
							console.log(err);
			        wx.showToast({
			          title: '上传失败',
			          icon: 'error'
			        })
			      }
			    })
			  })
			}
		},
		onHide() {
			
		},
		onUnload() {
			Audio.audioCtx.stop();
			equipment.stop(()=>{})
			Audio.audioCtx.destroy();
		}
	}
</script>

<style lang="scss" scoped>

</style>
